package com.century.sjt.util.volleyUtil;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;

public class LruImageCache implements ImageCache {

    private static LruCache<String, Bitmap> mMemoryCache;
    private static LruImageCache lruImageCache;

    public LruImageCache() {
        int maxMemory=(int) Runtime.getRuntime().maxMemory();
        int cacheSize = maxMemory / 8;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getRowBytes() * bitmap.getHeight();
            }
        };
    }

    public static LruImageCache instance(){
        if(lruImageCache==null){
            lruImageCache=new LruImageCache();
        }
        return lruImageCache;
    }

    @Override
    public Bitmap getBitmap(String url) {
        // TODO Auto-generated method stub
        return mMemoryCache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        mMemoryCache.put(url, bitmap);

    }

}
